<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
include_once("Common.php");
include_once("mysqlha.php");
header("Content-Type:text/html; charset=utf-8");
class FareController extends Controller {

    public function addFare(){
        $Model = M();
        $classifyList = $Model->Query("select * from deliverytime");
		$shopList = $Model->Query("select shopid,name from shop");
        $firstshop = array('shopid' => 0, 'name' => '所有商店' );
        array_unshift($shopList,$firstshop); 
        $this->assign('classifyList',$classifyList);
		$this->assign('shopList',$shopList);
        $this->display("addFare.php");
    }

    public function fareList(){
        $Model = M();
        $goods = $Model->Query("SELECT fareml.*,CASE WHEN fareml.`shopid` = 0 THEN '所有商家' ELSE shop.name END AS shopname FROM fareml LEFT JOIN shop ON fareml.shopid = shop.shopid");

        $this->assign('goods',$goods);
        $this->display("FareList.php"); 
    }

    public function ChangeOne(){
        $id = $_REQUEST['id'];
        $Model = M();

        $Obj = $Model->Query("select * from fareml where id=".$id."");
        $muList = $Model->Query("select * from fare where muid=".$id." and area!='0'");
        $mo = $Model->Query("select * from fare where muid=".$id." and area=0");
        $classifyList = $Model->Query("select * from deliverytime");
		$shopList = $Model->Query("select * from shop");
        $firstshop = array('shopid' => 0, 'name' => '所有商店' );
        array_unshift($shopList,$firstshop); 
		$this->assign('shopList',$shopList);
        $this->assign('classifyList',$classifyList);
        $this->assign('Obj',$Obj[0]);
        $this->assign('addList',$muList);
        $this->assign('mo',$mo[0]);
        $this->display('changeFare.php');
    }

    public function myChange(){
    $quantityA = $_REQUEST['quantity'];
    $begincountA = $_REQUEST['begincount'];
    $deliverytime = $_REQUEST['deliverytime'];
    $fare = $_REQUEST['fare'];
    $addfare = $_REQUEST['addfare'];
    $area = $_REQUEST['area'];
    $type = $_REQUEST['Paymethod'];
    $isPay = $_REQUEST['isPay'];
    $name = $_REQUEST['name'];
	$shopid = $_REQUEST['shopid'];

    $muid = $_REQUEST['muid'];
    del('fare',"muid=".$muid."");
    $updateArr = array(
        "deliverytime"=>$deliverytime,
        "name"=>$name,
		"shopid"=>$shopid
    );
    update("fareml",$updateArr,"id=".$muid."");

    if($isPay == 1){
        $returnArr = array(
            "code"=>200,
            "callbackType"=>"closeCurrent",
            "navTabId"=>"fare_list",
            "forwardUrl"=>"index.php?c=Fare&a=fareList",
            "msg"=>"插入成功"
        );
        echo json_encode($returnArr);
        exit();
    }

    for ($i = 0; $i < sizeof($quantityA); $i++) {
        $insertArr = array(
            "begincount" => $begincountA[$i],
            "fare" => $fare[$i],
            "addfare" => $addfare[$i],
            "area" => $area[$i],
            "muid" => $muid
        );
        myinsert("fare", $insertArr);
    }

    $returnArr = array(
        "code"=>200,
        "callbackType"=>"closeCurrent",
        "navTabId"=>"fare_list",
        "forwardUrl"=>"index.php?c=Fare&a=fareList",
        "msg"=>"插入成功"
    );
    echo json_encode($returnArr);
}

    //获取订单
    public function getOrderFare(){
        $prov = $_REQUEST['prov'];
        $city = $_REQUEST['city'];
        $userid = $_REQUEST['userid'];

        $Model = M();
        $goods = $Model->Query("select t.*,t1.muid,t1.weight from car t,goods t1 where t.fk_userid=".$userid." and t.mselect=1 and t.fk_goodsid=t1.id");
        $money = 0;
        for($i=0;$i<sizeof($goods);$i++){
            $id = $goods[$i]['muid'];
            $number = (int)$goods[$i]['number'];
			$weight = $goods[$i]['weight'];
			$weight = $weight*$number;
            $oneMoney = array();
            $Obj = $Model->Query("select * from fareml where id=".$id."");
            $Obj = $Obj[0];

            $cR = $Model->Query("SELECT * FROM fare WHERE AREA LIKE '%".$city."%' and muid=".$id."");

            if(sizeof($cR)>0){
                $oneMoney = $cR;

            }else{

                $pR = $Model->Query("SELECT * FROM fare WHERE AREA LIKE '%".$prov."' and muid=".$id."");
                if(sizeof($pR)>0){
                    $oneMoney = $pR;
                }else{
                    $oneMoney= $Model->Query("SELECT * FROM fare WHERE area=0 and muid=".$id."");
                }
            }


            if($Obj['type'] == 0){
                $addFare = (float)$oneMoney[0]['addfare'];
                $fare = (float)$oneMoney[0]['fare'];
				$begincount = (int)$oneMoney[0]['begincount'];

				$acount = $number-$begincount;

                $allOne = $acount*$addFare+$fare;
				$money+=$allOne;
            }else if($Obj['type'] == 1){
				$addFare = $oneMoney[0]['addfare'];
                $fare = (float)$oneMoney[0]['fare'];
				$begincount = $oneMoney[0]['begincount'];
				$acount = $weight-$begincount;
				$allOne = $acount*$addFare+$fare;
				$money+=$allOne;
			}


        }
		$returnArr = array(
		    "code"=>200,
			"yuMoney"=>$money
		);
		
		echo json_encode($returnArr);
    }

    public function getFare(){
        $prov = $_REQUEST['prov'];
        $city = $_REQUEST['city'];
        $id = $_REQUEST['id'];
        $city =substr($city,1,-1);
        $prov =substr($prov,1,-1);
        $Model = M();
        $cR = $Model->Query("SELECT * FROM fare WHERE AREA LIKE '%".$city."%' and muid=".$id."");
        if(sizeof($cR)>0){
            $returnArr = array(
               "code"=>200,
                "money"=>$cR[0]['fare']
            );
            echo json_encode($returnArr);
            exit();
        }else{
            $pR = $Model->Query("SELECT * FROM fare WHERE AREA LIKE '%".$prov."%' and muid=".$id."");
            if(sizeof($pR)>0) {
                $returnArr = array(
                    "code" => 200,
                    "money" => $pR[0]['fare']
                );
                echo json_encode($returnArr);
                exit();
            }
        }
        $money = $Model->Query("SELECT * FROM fare WHERE area=0 and muid=".$id."");
        $returnArr = array(
            "code" => 200,
            "money" => $money[0]['fare']
        );
        echo json_encode($returnArr);
    }

    public function delOne(){
        $id = $_REQUEST['id'];

        del('fareml',"id=".$id."");
        $returnArr = array(
            "code"=>200,
            "msg"=>"删除成功"
        );
        echo json_encode($returnArr);
    }

    public function AddOne(){
        $quantityA = $_REQUEST['quantity'];
        $begincountA = $_REQUEST['begincount'];
        $deliverytime = $_REQUEST['deliverytime'];
        $fare = $_REQUEST['fare'];
        $addfare = $_REQUEST['addfare'];
        $fare1 =  $_REQUEST['fare1'];
        $addfare1 = $_REQUEST['addfare1'];
        $area1 = $_REQUEST['area1'];
        $begincountA1 = $_REQUEST['begincount1'];
        $quantityA1 = $_REQUEST['quantity1'];
        $area = $_REQUEST['area'];
        $type = $_REQUEST['Paymethod'];
        $isPay = $_REQUEST['isPay'];
        $name = $_REQUEST['name'];
		$shopid = $_REQUEST['shopid'];

        $insertArr = array(
            "isPay"=>$isPay,
            "deliverytime"=>$deliverytime,
            "name"=>$name,
            "type"=>$type,
			"shopid"=>$shopid
        );
        $muid = myinsert("fareml",$insertArr);
        if($isPay == 1){
            $returnArr = array(
                "code"=>200,
                "callbackType"=>"closeCurrent",
                "navTabId"=>"fare_list",
                "forwardUrl"=>"index.php?c=Fare&a=fareList",
                "msg"=>"插入成功"
            );
            echo json_encode($returnArr);
            exit();
        }
        if($type == 1) {
            for ($i = 0; $i < sizeof($quantityA); $i++) {
                $insertArr = array(
                    "begincount" => $begincountA[$i],
                    "fare" => $fare[$i],
                    "addfare" => $addfare[$i],
                    "area" => $area[$i],
                    "muid" => $muid
                );
                myinsert("fare", $insertArr);
            }
        }else{
            for ($i = 0; $i < sizeof($quantityA1); $i++) {
                $insertArr = array(
                    "begincount" => $begincountA1[$i],
                    "fare" => $fare1[$i],
                    "addfare" => $addfare1[$i],
                    "area" => $area1[$i],
                    "muid" => $muid
                );
                myinsert("fare", $insertArr);
            }
        }
        $returnArr = array(
            "code"=>200,
            "callbackType"=>"closeCurrent",
            "navTabId"=>"fare_list",
            "forwardUrl"=>"index.php?c=Fare&a=fareList",
            "msg"=>"插入成功"
        );
        echo json_encode($returnArr);

    }

}